import http from './http'

// 导入银行流水
export const importTransactions = async (file: File, fieldMapping: Record<string, string>) => {
  try {
    const formData = new FormData();
    formData.append('file', file);
    formData.append('fieldMapping', JSON.stringify(fieldMapping));
    
    const response = await http.post('/bank-transactions/import', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
    return {
      success: true,
      data: response.data
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '导入银行流水失败'
    };
  }
}

// 导出银行流水模板
export const exportTransactionTemplate = async () => {
  try {
    const response = await http.get('/bank-transactions/export-template', {
      responseType: 'blob'
    });
    return {
      success: true,
      data: response.data
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '导出银行流水模板失败'
    };
  }
}

// 获取银行流水列表
export const getTransactions = async (params: any) => {
  try {
    const response = await http.get('/bank-transactions', { params });
    return {
      success: true,
      data: response.data
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '获取银行流水列表失败'
    };
  }
}

// 获取银行流水详情
export const getTransactionDetail = async (id: string) => {
  try {
    const response = await http.get(`/bank-transactions/${id}`);
    return {
      success: true,
      data: response.data
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '获取银行流水详情失败'
    };
  }
}

// 获取银行流水统计信息
export const getTransactionStats = async () => {
  try {
    const response = await http.get('/bank-transactions/stats');
    return {
      success: true,
      data: response.data
    };
  } catch (error: any) {
    return {
      success: false,
      data: null,
      message: error.message || '获取银行流水统计信息失败'
    };
  }
}